package io.github.xbeeant.eoffice.service.impl;

import io.github.xbeeant.core.ApiResponse;
import io.github.xbeeant.core.IdWorker;
import io.github.xbeeant.eoffice.config.AbstractSecurityMybatisPageHelperServiceImpl;
import io.github.xbeeant.eoffice.mapper.ResourcesVersionMapper;
import io.github.xbeeant.eoffice.model.ResourcesVersion;
import io.github.xbeeant.eoffice.onlyoffice.OfficeHistory;
import io.github.xbeeant.eoffice.onlyoffice.OfficeHistoryData;
import io.github.xbeeant.eoffice.po.ResourcesVersionPo;
import io.github.xbeeant.eoffice.service.IResourcesVersionService;
import io.github.xbeeant.eoffice.util.FileHelper;
import io.github.xbeeant.spring.mybatis.pagehelper.IMybatisPageHelperDao;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 用户资源版本历史
 */
@Service
public class ResourcesVersionServiceImpl extends AbstractSecurityMybatisPageHelperServiceImpl<ResourcesVersion, Long> implements IResourcesVersionService {

    @Autowired
    private ResourcesVersionMapper resourcesVersionMapper;

    @Override
    public IMybatisPageHelperDao<ResourcesVersion, Long> getRepositoryDao() {
        return this.resourcesVersionMapper;
    }

    @Override
    public ApiResponse<ResourcesVersion> insertSelective(ResourcesVersion record) {
        // 获取数据库中独最新版本号 + 1 后就是当前的版本号
        // 如果数据库中没有 rid对应的版本 那就是版本0 0 表示文件才新建
        Long rid = record.getRid();
        ResourcesVersion latest = resourcesVersionMapper.latest(rid);
        if (null == latest) {
            record.setVersion(0);
        } else {
            record.setVersion(latest.getVersion() + 1);
        }
        return super.insertSelective(record);
    }

    @Override
    public void setDefaults(ResourcesVersion record) {
        if (record.getVid() == null) {
            record.setVid(IdWorker.getId());
        }
    }

    @Override
    public ApiResponse<OfficeHistory> history(Long rid) {
        ApiResponse<OfficeHistory> result = new ApiResponse<>();
        List<ResourcesVersionPo> histories = resourcesVersionMapper.history(rid);
        if (CollectionUtils.isEmpty(histories)) {
            result.setResult(1, "该文件没有变更历史");
            return result;
        }
        OfficeHistory officeHistory = new OfficeHistory();
        for (ResourcesVersionPo datum : histories) {
            officeHistory.addHistory(datum);
        }
        result.setData(officeHistory);
        return result;
    }

    @Override
    public ApiResponse<OfficeHistoryData> historyData(Long rid, Integer version, String sessionId) {
        // todo 资源权限校验
        ApiResponse<OfficeHistoryData> result = new ApiResponse<>();
        ResourcesVersion resourcesVersion = resourcesVersionMapper.version(rid, version);
        OfficeHistoryData historyData = new OfficeHistoryData();
        historyData.setChangesUrl(FileHelper.changesUrl(resourcesVersion.getVid(), sessionId));
        historyData.setKey(String.valueOf(resourcesVersion.getVid()));
        historyData.setUrl(FileHelper.downloadUrl(rid, resourcesVersion.getVid(), sessionId));
        historyData.setVersion(version);
        if (version > 0) {
            ResourcesVersion preVersion = resourcesVersionMapper.version(rid, version - 1);
            OfficeHistoryData.Previous previous = new OfficeHistoryData.Previous();
            previous.setKey(String.valueOf(preVersion.getKey()));
            previous.setUrl(FileHelper.downloadUrl(rid, preVersion.getVid(), sessionId));
            historyData.setPrevious(previous);
        }
        result.setData(historyData);
        return result;
    }
}
